import React, {Component} from 'react';
import { Button, Form, Input } from 'antd';

class EditDept extends Component {
  constructor(props) {
    super(props);
    this.formRef = React.createRef();
    this.state={
    }
  }
  componentWillReceiveProps(nextProps, nextContext) {
    console.log("nextPros", nextProps);
    if (nextProps.currentData.deptId!=this.props.currentData.deptId){
      this.formRef.current.setFieldsValue(nextProps.currentData);
      this.setState({
        currentData:nextProps.currentData,
      })
    }
  }
  componentDidMount() {
    this.formRef.current.setFieldsValue(this.props.currentData);
    this.setState({
        currentData:this.props.currentData,
      }
    )
  }

  onFinish = (values) => {
    console.log("onFinish");
    let url = "http://1.94.134.166:8805/dept";
    values.parentId=0;
    const sendData={...this.state.currentData,...values}
    fetch(url,{
      method:"PUT",
      body:JSON.stringify(sendData),
      headers:{
        'Content-Type': 'application/json',
        Authorization:localStorage.getItem('Authorization')
      },
    })
      .then((response)=>response.json())
      .then((res)=>{
        console.log(res);
        const {code=0}=res;
        if(code==0){
          console.log(res);
          this.props.refresh();
          this.props.closeDrawer();
          //this.formRef.current.resetFields();
        }
      });
  };

  render() {
    console.log(this.props);
    return(
      <>
        <Form
          name="normal_login"
          className="login-form"
          onFinish={this.onFinish}
          ref={this.formRef}

        >
          <Form.Item
            name="deptName"
            label="部门名称"
            rules={[
              {
                required: true,
                message: '请输入部门名称！',
              },
              {
                pattern:/[\u4e00-\u9fa5]+/,
                message:'请输入正确的部门名称!',
              },
            ]}
          >
            <Input placeholder="部门名称" />
          </Form.Item>
          <Form.Item
            name="deptPy"
            label="部门拼音"
            rules={[
              {
                required: true,
                message: '请输入部门拼音!',
              },
              {
                pattern:/[bpmf|dtnl|gkh|jqx|zhchshrzcsyw][aeiouü]*/,
                message:'请输入正确的部门拼音!',
              }
            ]}
          >
            <Input
              placeholder="部门拼音"
            />
          </Form.Item>
          <Form.Item
            name="phone"
            label="部门联系人电话"
            rules={[
              {
                required: true,
                message: '请输入部门联系人电话!',
              },
              {
                pattern:/^1[3-9]\d{9}$/,
                message:'请输入正确的手机号码!',
              },
            ]}
          >
            <Input
              placeholder="部门联系人电话"
            />
          </Form.Item>
          <Form.Item>
            <Button type="primary" htmlType="submit" className="login-form-button">
              确认修改
            </Button>
          </Form.Item>
        </Form>
      </>
    )
  }
}
export default EditDept;
